package com.titaniue.java1;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @program: JavaSenior
 * @description: 实现从客户端发送文件给服务端，服务端保存到本地。并返回“发送成功” 并关闭想赢连接
 * @author: renfei.huang
 * @create: 2021-10-25 20:12
 */
public class TCPTest3 {


    @Test
    public void client() throws IOException {

        Socket socke = null;
        OutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            socke = new Socket(InetAddress.getByName("127.0.0.1"), 9090);

            outputStream = socke.getOutputStream();

            fileInputStream = new FileInputStream(new File("zzz.jpg"));

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }

            //关闭数据输出
            socke.shutdownOutput();

            //接收服务端的消息，并显示
            InputStream inputStream = socke.getInputStream();
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bufferr = new byte[20];
            int len1;
            while ((len1 = inputStream.read(bufferr)) != -1) {
                byteArrayOutputStream.write(buffer, 0, len1);
            }
            byte[] bytes = byteArrayOutputStream.toByteArray();

            String s = new String(bytes);

            System.out.println(s);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (socke != null) {
                try {
                    socke.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }


    @Test
    public void server() throws IOException {

        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        OutputStream outputStream = null;
        try {
            //1.
            serverSocket = new ServerSocket(9090);
            //2.
            socket = serverSocket.accept();
            //3.
            inputStream = socket.getInputStream();
            //4.
            fileOutputStream = new FileOutputStream(new File("zzz2.jpg"));

            //5.
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }


            System.out.println("图片传输完成");
            //6.服务端给客户端发送消息反馈
            outputStream = socket.getOutputStream();
            outputStream.write("你好，我收到了照片".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
